﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Electure.DAL;

namespace Electure
{
    public partial class VideoLectures : System.Web.UI.Page
    {
        int categoryId
        {
            get
            {
                int id;
                if (!int.TryParse(Request.QueryString["categoryId"], out id))
                    return -1;
                return id;
            }
        }
        int scienceDepartmentId
        {
            get
            {
                int id;
                if (!int.TryParse(Request.QueryString["scienceDepartmentId"], out id))
                    return -1;
                return id;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack) return;

            using (var rep = RepositoryFactory.GetCategoryRepository())
            {
                ddlCategory.Items.Add(new ListItem() { Value = "", Text = "Оберіть категорію" });
                rep.GetAll().ForEach(item => ddlCategory.Items.Add(new ListItem() { Value = item.ID.ToString(), Text = item.Name }));
            }
            using (var rep = RepositoryFactory.GetScienceDepartmentRepository())
            {
                var departments = rep.GetAll();
                ddlScienceDepartment.Items.Add(new ListItem() { Value = "", Text = "Оберіть навчальний підрозділ" });
                departments.ForEach(item => ddlScienceDepartment.Items.Add(new ListItem() { Value = item.ID.ToString(), Text = item.Name }));
            }

            ddlCategory.SelectedValue = categoryId.ToString();
            ddlScienceDepartment.SelectedValue = scienceDepartmentId.ToString();

            using (var rep = RepositoryFactory.GetPublicationRepository())
            {
                publicationsList.Content = rep.GetAll(categoryId, scienceDepartmentId);
            }
        }

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            string url = "VideoLectures.aspx", param = "";
            if (!string.IsNullOrEmpty(ddlScienceDepartment.SelectedValue))
                param += "scienceDepartmentId=" + ddlScienceDepartment.SelectedValue;
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))
                param += (param.Length > 0 ? "&" : "") + "categoryId=" + ddlCategory.SelectedValue;
            Response.Redirect(url + (param.Length > 0 ? "?" + param : ""));
        }
    }
}